import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2022/11/22 15:47
 */

class User{
    public int userId;
    public int classId;
}


@WebServlet("/postJson")
public class PostJsonServlet extends HttpServlet {
    //1.创建jackson 核心对象
   private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       User user = objectMapper.readValue(req.getInputStream(),User.class);
       //User.class 根据 自省 来实现匹配
        resp.getWriter().write("userId="+user.userId+",classId="+user.classId);
    }
}
